﻿Option Explicit On
Option Strict On


Imports ACOMDirector.Hardware
Imports ACOMDirector.Hardware.ACOM2000

Public Class VirtualACOM2000

    'ACOM State Variables
    Public m_CurrentSegment As Integer = 163
    Public m_CurrentUSR As Integer = 1
    Public m_CurrentBand As Integer = 0
    Public m_CurrentAntenna As Integer = 1






    Private WithEvents m_RS232 As New ACOMDirector.Hardware.RS232.Port 'The RS232 Connection
    Private m_COMPort As String 'The COM Port to use to connect
    Private m_Buffer() As Byte = New Byte() {} 'The Buffer used for incoming data from the ACOM2000A

    Private WithEvents m_BackgroundSenderTimer As New Timers.Timer(1000)

    'ACOM RS232 Network Addresses
    Private m_ACOMNetworkACOM2000AAddress As Integer = &H41 'The Address of the ACOM2000A on the ACOM RS232 Network
    Private m_ACOMNetworkConnectionAddress As Integer = &H71 'The Address of the PC Controller on the ACOM RS232 Network

    Public Event COMDataReceivedEvent As EventHandler(Of UnknownMessageEventArgs)

#Region " Public Methods "

    Public Sub Start(ByVal Port As String)

        m_COMPort = Port

        'Clear the Buffer
        m_Buffer = New Byte() {}

        'Force the COM Port closed just in case
        RS232.Port.ForcePortClosed(m_COMPort)

        'Open the RS232 Port
        Dim RS232Parameters As New RS232.PortParameters(1200, m_COMPort, False, False, IO.Ports.StopBits.One, 8, IO.Ports.Handshake.None, IO.Ports.Parity.None)
        m_RS232.Connect(RS232Parameters)

        m_BackgroundSenderTimer.Start()

    End Sub

    Public Sub [Stop]()

        If m_RS232.IsOpen Then
            m_RS232.Disconnect()
        End If

    End Sub

    Public Sub SendRandom(ByVal Text As String)
        m_RS232.SendData(Text)
    End Sub



#End Region


    ''' <summary>
    ''' Sends a message to the ACOM2000A network
    ''' </summary>
    ''' <param name="Message"></param>
    ''' <remarks></remarks>
    Public Sub SendMessage(ByVal Message As String, ByVal LogData As Boolean)
        SendMessage(GenerateMessageFrame(Message), LogData)
    End Sub

    ''' <summary>
    ''' Sends a message to the ACOM2000A network
    ''' </summary>
    ''' <param name="Message"></param>
    ''' <remarks></remarks>
    Private Sub SendMessage(ByVal Message As Byte(), ByVal LogData As Boolean)
        If m_RS232.IsOpen Then
            m_RS232.SendData(Message)

            If LogData Then
                RaiseEvent COMDataReceivedEvent(Me, New UnknownMessageEventArgs("> - " & System.Text.ASCIIEncoding.ASCII.GetString(Message)))
            End If
        End If
    End Sub

    ''' <summary>
    ''' Generates a message frame with the appropriate Destination, Source and Terminator bytes
    ''' </summary>
    ''' <param name="Command"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function GenerateMessageFrame(ByVal Command As String) As Byte()

        Dim Frame As New List(Of Byte)

        Frame.Add(CByte(m_ACOMNetworkConnectionAddress)) 'Destination Address
        Frame.Add(CByte(m_ACOMNetworkACOM2000AAddress)) 'Source Address
        For Each c As Char In Command.ToCharArray
            Frame.Add(CByte(Asc(c)))
        Next
        Frame.Add(0) 'Message Terminator
        Return Frame.ToArray

    End Function

    ''' <summary>
    ''' Generates a message frame with the appropriate Broadcast, Source and Terminator bytes
    ''' </summary>
    ''' <param name="Command"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function GenerateBroadcastMessageFrame(ByVal Command As String) As Byte()

        Dim Frame As New List(Of Byte)
        Frame.Add(Constants.ACOMNETWORK_BROADCAST_ADDRESS) 'Destination Address
        Frame.Add(CByte(m_ACOMNetworkACOM2000AAddress)) 'Source Address
        For Each c As Char In Command.ToCharArray
            Frame.Add(CByte(Asc(c)))
        Next
        Frame.Add(0) 'Message Terminator
        Return Frame.ToArray

    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="Number"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Shared Function EncodeACOMFormatNumber(ByVal Number As Integer) As String

        Dim LowByte As Integer = Number And &HF0
        Dim HighByte As Integer = Number And &HF

        Return Chr((LowByte \ 16) + 48) & Chr(HighByte + 48)

    End Function


#Region " RS232 Handlers "

    ''' <summary>
    ''' Data Received from the RS232 connection
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub m_RS232_DataReceived(ByVal sender As Object, ByVal e As RS232.DataReceivedEventArgs) Handles m_RS232.DataReceived
        DecodeMessages(e.ByteData)
        RaiseEvent COMDataReceivedEvent(Me, New UnknownMessageEventArgs("< - " & e.StringData))
    End Sub

    Private Sub m_RS232_PortClosed(sender As Object, e As System.EventArgs) Handles m_RS232.PortClosed
        'm_TimeoutTimer.Stop()

        'TODO: Should probably have a way of giving a connection error as this is the only reason why this event would be called
    End Sub

#End Region


#Region " Message Processor "

    Public Sub SendForwardPower(ByVal Value As Integer)

        Dim Data As Byte() = New Byte() {CByte(128 + Value)}
        SendMessage(Data, False)

    End Sub

    Public Sub SendReflectedPower(ByVal Value As Integer)

        Dim Data As Byte() = New Byte() {CByte(160 + Value)}
        SendMessage(Data, False)

    End Sub

    ''' <summary>
    ''' Decodes the Messages received from the COM Port and Sends them to the message processor
    ''' </summary>
    ''' <param name="Data"></param>
    ''' <remarks></remarks>
    Public Sub DecodeMessages(ByVal Data() As Byte)

        'Check Data Isn't Nothing 
        If Data Is Nothing Then
            Throw New ArgumentNullException("Data")
        End If

        Dim MessageList As New Generic.List(Of Message)

        Dim OldBufferLength As Integer = m_Buffer.Length
        Array.Resize(m_Buffer, m_Buffer.Length + Data.Length)
        Array.Copy(Data, 0, m_Buffer, OldBufferLength, Data.Length)

        'Split up commands
        Dim CommandStart As Integer = 0
        For i As Integer = 0 To m_Buffer.GetUpperBound(0)
            If (m_Buffer(i) >= Constants.FORWARD_POWER_LED_MESSAGE_LOWESTVALUE And m_Buffer(i) <= Constants.FORWARD_POWER_LED_MESSAGE_HIGESTVALUE) Or (m_Buffer(i) >= Constants.REFLECTED_POWER_LED_MESSAGE_LOWESTVALUE And m_Buffer(i) <= Constants.REFLECTED_POWER_LED_MESSAGE_HIGESTVALUE) Then
                Dim CommandData(0) As Byte
                CommandData(0) = m_Buffer(i)
                MessageList.Add(New Message(CommandData))
                CommandStart = i + 1
            ElseIf m_Buffer(i) = 0 Or m_Buffer(i) = 13 Then '&H00 and &H0D are end of command terminators
                Dim CommandData((i - CommandStart) - 1) As Byte
                Array.Copy(m_Buffer, CommandStart, CommandData, 0, CommandData.Length)
                If CommandData.Length > 0 Then
                    MessageList.Add(New Message(CommandData))
                End If
                CommandStart = i + 1
            End If
        Next

        'If this is the end of the buffer then clear the buffer
        If CommandStart = m_Buffer.Length Then
            m_Buffer = New Byte() {}
        ElseIf CommandStart = 0 Then
            'Do nothing, the whole buffer needs storing
        Else 'Save the remainder for processing next 
            Dim WorkingBuffer() As Byte = DirectCast(m_Buffer.Clone, Byte())
            Array.Resize(m_Buffer, m_Buffer.Length - CommandStart)
            Array.Copy(WorkingBuffer, CommandStart, m_Buffer, 0, m_Buffer.Length)
        End If

        'Process the commands
        ProcessMessages(MessageList)

    End Sub

    ''' <summary>
    ''' Processes Messages Received from the COM Port
    ''' </summary>
    ''' <param name="MessageList"></param>
    ''' <remarks></remarks>
    Private Sub ProcessMessages(ByVal MessageList As Generic.List(Of Message))

        For Each Message As Message In MessageList

            'Is the message intended for me?
            If Not Message.DestinationAddress = m_ACOMNetworkACOM2000AAddress And Not Message.DestinationAddress = Constants.ACOMNETWORK_BROADCAST_ADDRESS Then
                Exit Sub
            End If

                       Select Message.MessageType
                '                Case "FORWARDPOWERLED"
                '                    HandleMessageForwardPowerLED(Message)
                '                Case "REFLECTEDPOWERLED"
                '                    HandleMessageReflectedPowerLED(Message)
                '                Case "A" 'BAND Motor Position
                '                    HandleMessageBANDMotorPosition(Message)
                '                Case "B" 'LOAD Motor Position
                '                    HandleMessageLOADMotorPosition(Message)
                '                Case "C" 'TUNE Motor Position
                '                    HandleMessageTUNEMotorPosition(Message)
                '                Case "E" 'Amplifier Error
                '                    HandleMessageAmplifierError(Message)
                '                Case "G1" 'SRAM Contents
                '                    'TODO: Add support for G1-SRAM Content message type
                '                Case "H" 'Motors are in home position
                '                    'TODO: Add support for H-Motors in home position message type
                '                Case "I" 'Air Temperature (x 10deg C), LCD Bars Position
                '                    HandleMessageAirTemperature(Message)
                '                Case "L" 'This is the Broadcast address identifier message
                '                    HandleMessageBroadcastID(Message)
                '                Case "R" 'Amplifier Individual Data
                '                    'TODO: Add support for R-Amplifier individual data message type
                '                Case "P" 'Input Power Marker Position
                '                    'TODO: Add support for P-Input power marker message type
                '                Case "Q" 'Autotune
                '                    'TODO: Add support for Q-Autotune message type
                '                Case "Q3" 'Preceeds Error Message when not in auto tune
                '                    'TODO: Add support for Q3 message type
                '                Case "V" 'Error flag?
                '                    'TODO: Add support for V message type
                '                Case "G3" 'EEPROM Contents
                '                    HandleMessageEEPROMContents(Message)
                '                Case "O" 'Operate Mode On
                '                    HandleMessageOperateModeOn()
                '                Case "S" 'StandBy Mode On
                '                    HandleMessageStandbyModeOn()
                '                Case "U" 'LED Status
                '                    HandleMessageLEDStatus(Message)
                Case "W7" 'Current Frequency Segment Requested
                    SendCurrentFrequencySegmentInfo_W7


                    '                Case "MM" 'The RCU is in measuring mode
                    '                    HandleMessageRCUEnteredMeasureMode()
                    '                Case "0" 'Power Off Executed
                    '                    HandleMessagePowerOffExecuted()
                    '                Case "1" 'Power On Executed
                    '                    HandleMessagePowerOnExecuted()
                    '                Case "2" 'HV On
                    '                    HandleMessageHVOn()
                    '                Case "3" 'Power Off But still cooling tubes
                    '                    HandleMessagePowerOffCoolingTubes()
                    '                Case "4" 'Power Off Complete
                    '                    HandleMessagePowerOffComplete()
                    '                Case "b" 'MeasurementMetricType.Bias '= xx/2,V
                    '                    HandleMessageMeasurementBias(Message)
                    '                Case "e" 'MeasurementMetricType.DCInput '= xx*yy*0,16, W
                    '                    HandleMessageMeasurementDCInput(Message)
                    '                Case "f" 'MeasurementMetricType.Drive '= xx*xx/512, W
                    '                    HandleMessageMeasurementDrive(Message)
                    '                Case "h" 'MeasurementMetricType.Frequency '= (xxyy)*2, kHz
                    '                    HandleMessageMeasurementFrequency(Message)
                    '                Case "i" 'MeasurementMetricType.ForwardPower '= xx*xx/16, W
                    '                    HandleMessageMeasurementForwardPower(Message)
                    '                Case "j" 'MeasurementMetricType.FilamentVoltage '= xx/16, V
                    '                    HandleMessageMeasurementFilamentVoltage(Message)
                    '                Case "k" 'MeasurementMetricType.PowerGain '= 10*lg(xx*xx/yy/yy*32), dB
                    '                    HandleMessageMeasurementPowerGain(Message)
                    '                Case "l" 'MeasurementMetricType.HV '= xx*16, V
                    '                    HandleMessageMeasurementHV(Message)
                    '                Case "m" 'MeasurementMetricType.IFFrontTube '= xx/16, A
                    '                    HandleMessageMeasurementIFFrontTube(Message)
                    '                Case "n" 'MeasurementMetricType.IFRearTube '=xx/16, A
                    '                    HandleMessageMeasurementIFRearTube(Message)
                    '                Case "o" 'MeasurementMetricType.GridCurrent '= xx/10, mA
                    '                    HandleMessageMeasurementGridCurrent(Message)
                    '                Case "p" 'MeasurementMetricType.PlateCurrent '= xx*10, mA
                    '                    HandleMessageMeasurementPlateCurrent(Message)
                    '                Case "q" 'MeasurementMetricType.ScreenCurrent '= xx, mA
                    '                    HandleMessageMeasurementScreenCurrent(Message)
                    '                Case "r" 'MeasurementMetricType.MainsVoltage '= xx*10/8, V
                    '                    HandleMessageMeasurementMainsVoltage(Message)
                    '                Case "s" 'MeasurementMetricType.PlateRFVoltage '= xx*12, V
                    '                    HandleMessageMeasurementPlateRFVoltage(Message)
                    '                Case "t" 'MeasurementMetricType.ReflectedPower '= xx*xx/100, W
                    '                    HandleMessageMeasurementReflectedPower(Message)
                    '                Case "w" 'MeasurementMetricType.ScreenVoltage '= xx*2, V
                    '                    HandleMessageMeasurementScreenVoltage(Message)
                    '                Case "x" 'MeasurementMetricType.FrontTubeTemp '= (xx-136)*2, oC
                    '                    HandleMessageMeasurementFrontTubeTemp(Message)
                    '                Case "y" 'MeasurementMetricType.RearTubeTemp '= (xx-136)*2, oC
                    '                    HandleMessageMeasurementRearTubeTemp(Message)
                    '                Case "z" 'MeasurementMetricType.VSWR '= (2,5*xx+yy)/(2,5*xx-yy)
                    '                    HandleMessageMeasurementVSWR(Message)
                    '                Case Else
                    '                    'Unknown Message so print debug info
                    '                    Dim DebugString As String = ""
                    '                    DebugString &= "From: " & Hex(Message.SourceAddress)
                    '                    DebugString &= " To: " & Hex(Message.DestinationAddress)
                    '                    DebugString &= " Command: " & Message.MessageType
                    '                    DebugString &= " Data: "
                    '                    If Message.Parameters IsNot Nothing Then
                    '                        For i As Integer = 0 To Message.Parameters.GetUpperBound(0)
                    '                            If Not i = 0 Then
                    '                                DebugString &= ":"
                    '                            End If
                    '                            DebugString &= Hex(Message.Parameters(i))
                    '                        Next
                    '                    End If

                    '                    RaiseEvent UnknownMessage(Me, New UnknownMessageEventArgs(DebugString))
            End Select

        Next

    End Sub

    Public Sub SendCurrentFrequencySegmentInfo_W7()

        Dim SegmentNumber As String = EncodeACOMFormatNumber(m_CurrentSegment)
        Dim AntennaNumber As String = EncodeACOMFormatNumber(m_CurrentAntenna)
        Dim USR As String = m_CurrentUSR.ToString
        Dim Band As String = m_CurrentBand.ToString

        SendMessage(GenerateMessageFrame("W1" & SegmentNumber & AntennaNumber & USR & Band), True)

    End Sub


    '#Region " Command Handlers "

    '    Private Sub HandleMessageMeasurementBias(Message As Message)
    '        If MeasurementMetricUpdatedEvent IsNot Nothing Then
    '            Dim xx As Integer = ((Message.Parameters(0) - 48) * 16) + (Message.Parameters(1) - 48)
    '            Dim Value As Integer = CInt(xx / 2)
    '            RaiseEvent MeasurementMetricUpdated(Me, New MeasurementMetricUpdatedEventArgs(MeasurementMetricTypes.Bias, "-" & Value.ToString & "V"))
    '        End If
    '    End Sub

    '    Private Sub HandleMessageMeasurementDCInput(Message As Message)
    '        If MeasurementMetricUpdatedEvent IsNot Nothing Then
    '            Dim xx As Integer = ((Message.Parameters(0) - 48) * 16) + (Message.Parameters(1) - 48)
    '            Dim yy As Integer = ((Message.Parameters(2) - 48) * 16) + (Message.Parameters(3) - 48)

    '            Dim Value As Double = (xx * yy * 0.16) / 1000
    '            RaiseEvent MeasurementMetricUpdated(Me, New MeasurementMetricUpdatedEventArgs(MeasurementMetricTypes.DCInput, Value.ToString("0.00") & "kW"))
    '        End If
    '    End Sub

    '    Private Sub HandleMessageMeasurementDrive(Message As Message)
    '        If MeasurementMetricUpdatedEvent IsNot Nothing Then
    '            Dim xx As Integer = ((Message.Parameters(0) - 48) * 16) + (Message.Parameters(1) - 48)

    '            Dim Value As Integer = CInt(xx * xx / 512)
    '            RaiseEvent MeasurementMetricUpdated(Me, New MeasurementMetricUpdatedEventArgs(MeasurementMetricTypes.Drive, Value.ToString & "W"))
    '        End If
    '    End Sub

    '    Private Sub HandleMessageMeasurementFrequency(Message As Message)
    '        If MeasurementMetricUpdatedEvent IsNot Nothing Then
    '            Dim xx As Integer = ((Message.Parameters(0) - 48) * 4096) + ((Message.Parameters(1) - 48) * 256) + ((Message.Parameters(2) - 48) * 16) + (Message.Parameters(3) - 48)
    '            Dim Value As Integer = xx * 2
    '            RaiseEvent MeasurementMetricUpdated(Me, New MeasurementMetricUpdatedEventArgs(MeasurementMetricTypes.Frequency, Value.ToString("0000") & "kHz"))
    '        End If
    '    End Sub

    '    Private Sub HandleMessageMeasurementForwardPower(Message As Message)
    '        If MeasurementMetricUpdatedEvent IsNot Nothing Then
    '            Dim xx As Integer = ((Message.Parameters(0) - 48) * 16) + (Message.Parameters(1) - 48)
    '            Dim Value As Double = (xx * xx / 16) / 1000
    '            RaiseEvent MeasurementMetricUpdated(Me, New MeasurementMetricUpdatedEventArgs(MeasurementMetricTypes.ForwardPower, Value.ToString("0.00") & "kW"))
    '        End If
    '    End Sub

    '    Private Sub HandleMessageMeasurementFilamentVoltage(Message As Message)
    '        If MeasurementMetricUpdatedEvent IsNot Nothing Then
    '            Dim xx As Integer = ((Message.Parameters(0) - 48) * 16) + (Message.Parameters(1) - 48)
    '            Dim Value As Double = xx / 16
    '            RaiseEvent MeasurementMetricUpdated(Me, New MeasurementMetricUpdatedEventArgs(MeasurementMetricTypes.FilamentVoltage, Value.ToString("0.0") & "V"))
    '        End If
    '    End Sub

    '    Private Sub HandleMessageMeasurementPowerGain(Message As Message)
    '        If MeasurementMetricUpdatedEvent IsNot Nothing Then
    '            Dim xx As Integer = ((Message.Parameters(0) - 48) * 16) + (Message.Parameters(1) - 48)
    '            Dim yy As Integer = ((Message.Parameters(2) - 48) * 16) + (Message.Parameters(3) - 48)
    '            Dim OutputValue As String = ""
    '            If yy = 0 Then
    '                OutputValue = "----"
    '            Else
    '                Dim Value As Double = 10 * Math.Log10(xx * xx / yy / yy * 32)
    '                OutputValue = Value.ToString("0.0") & "dB"
    '            End If
    '            RaiseEvent MeasurementMetricUpdated(Me, New MeasurementMetricUpdatedEventArgs(MeasurementMetricTypes.PowerGain, OutputValue))
    '        End If
    '    End Sub

    '    Private Sub HandleMessageMeasurementHV(Message As Message)
    '        If MeasurementMetricUpdatedEvent IsNot Nothing Then
    '            Dim xx As Integer = ((Message.Parameters(0) - 48) * 16) + (Message.Parameters(1) - 48)
    '            Dim Value As Double = (xx * 16) / 1000
    '            RaiseEvent MeasurementMetricUpdated(Me, New MeasurementMetricUpdatedEventArgs(MeasurementMetricTypes.HV, Value.ToString("0.00") & "kV"))
    '        End If
    '    End Sub

    '    Private Sub HandleMessageMeasurementIFFrontTube(Message As Message)
    '        If MeasurementMetricUpdatedEvent IsNot Nothing Then
    '            Dim xx As Integer = ((Message.Parameters(0) - 48) * 16) + (Message.Parameters(1) - 48)
    '            Dim Value As Double = xx / 16
    '            RaiseEvent MeasurementMetricUpdated(Me, New MeasurementMetricUpdatedEventArgs(MeasurementMetricTypes.IFFrontTube, Value.ToString("0.0") & "A"))
    '        End If
    '    End Sub

    '    Private Sub HandleMessageMeasurementIFRearTube(Message As Message)
    '        If MeasurementMetricUpdatedEvent IsNot Nothing Then
    '            Dim xx As Integer = ((Message.Parameters(0) - 48) * 16) + (Message.Parameters(1) - 48)
    '            Dim Value As Double = xx / 16
    '            RaiseEvent MeasurementMetricUpdated(Me, New MeasurementMetricUpdatedEventArgs(MeasurementMetricTypes.IFRearTube, Value.ToString("0.0") & "A"))
    '        End If
    '    End Sub

    '    Private Sub HandleMessageMeasurementGridCurrent(Message As Message)
    '        If MeasurementMetricUpdatedEvent IsNot Nothing Then
    '            Dim xx As Integer = ((Message.Parameters(0) - 48) * 16) + (Message.Parameters(1) - 48)
    '            Dim Value As Double = xx / 10
    '            RaiseEvent MeasurementMetricUpdated(Me, New MeasurementMetricUpdatedEventArgs(MeasurementMetricTypes.GridCurrent, Value.ToString("0.0") & "mA"))
    '        End If
    '    End Sub

    '    Private Sub HandleMessageMeasurementPlateCurrent(Message As Message)
    '        If MeasurementMetricUpdatedEvent IsNot Nothing Then
    '            Dim xx As Integer = ((Message.Parameters(0) - 48) * 16) + (Message.Parameters(1) - 48)
    '            Dim Value As Double = xx * 10
    '            RaiseEvent MeasurementMetricUpdated(Me, New MeasurementMetricUpdatedEventArgs(MeasurementMetricTypes.PlateCurrent, Value.ToString("0.00") & "mA"))
    '        End If
    '    End Sub

    '    Private Sub HandleMessageMeasurementScreenCurrent(Message As Message)
    '        If MeasurementMetricUpdatedEvent IsNot Nothing Then
    '            Dim xx As Integer = ((Message.Parameters(0) - 48) * 16) + (Message.Parameters(1) - 48)
    '            Dim Value As Integer = xx
    '            RaiseEvent MeasurementMetricUpdated(Me, New MeasurementMetricUpdatedEventArgs(MeasurementMetricTypes.ScreenCurrent, Value.ToString & "mA"))
    '        End If
    '    End Sub

    '    Private Sub HandleMessageMeasurementMainsVoltage(Message As Message)
    '        If MeasurementMetricUpdatedEvent IsNot Nothing Then
    '            Dim xx As Integer = ((Message.Parameters(0) - 48) * 16) + (Message.Parameters(1) - 48)
    '            Dim Value As Integer = CInt(xx * 10 / 8)
    '            RaiseEvent MeasurementMetricUpdated(Me, New MeasurementMetricUpdatedEventArgs(MeasurementMetricTypes.MainsVoltage, Value.ToString() & "V"))
    '        End If
    '    End Sub

    '    Private Sub HandleMessageMeasurementPlateRFVoltage(Message As Message)
    '        If MeasurementMetricUpdatedEvent IsNot Nothing Then
    '            Dim xx As Integer = ((Message.Parameters(0) - 48) * 16) + (Message.Parameters(1) - 48)
    '            Dim Value As Double = (xx * 12) / 1000
    '            RaiseEvent MeasurementMetricUpdated(Me, New MeasurementMetricUpdatedEventArgs(MeasurementMetricTypes.PlateRFVoltage, Value.ToString("0.00") & "kV"))
    '        End If
    '    End Sub

    '    Private Sub HandleMessageMeasurementReflectedPower(Message As Message)
    '        If MeasurementMetricUpdatedEvent IsNot Nothing Then
    '            Dim xx As Integer = ((Message.Parameters(0) - 48) * 16) + (Message.Parameters(1) - 48)
    '            Dim Value As Integer = CInt(xx * xx / 100)
    '            RaiseEvent MeasurementMetricUpdated(Me, New MeasurementMetricUpdatedEventArgs(MeasurementMetricTypes.ReflectedPower, Value.ToString & "W"))
    '        End If
    '    End Sub

    '    Private Sub HandleMessageMeasurementScreenVoltage(Message As Message)
    '        If MeasurementMetricUpdatedEvent IsNot Nothing Then
    '            Dim xx As Integer = ((Message.Parameters(0) - 48) * 16) + (Message.Parameters(1) - 48)
    '            Dim Value As Integer = xx * 2
    '            RaiseEvent MeasurementMetricUpdated(Me, New MeasurementMetricUpdatedEventArgs(MeasurementMetricTypes.ScreenVoltage, Value.ToString & "V"))
    '        End If
    '    End Sub

    '    Private Sub HandleMessageMeasurementFrontTubeTemp(Message As Message)
    '        If MeasurementMetricUpdatedEvent IsNot Nothing Then
    '            Dim xx As Integer = ((Message.Parameters(0) - 48) * 16) + (Message.Parameters(1) - 48)
    '            Dim Value As Integer = (xx - 136) * 2
    '            RaiseEvent MeasurementMetricUpdated(Me, New MeasurementMetricUpdatedEventArgs(MeasurementMetricTypes.FrontTubeTemp, Value.ToString & "£C"))
    '        End If
    '    End Sub

    '    Private Sub HandleMessageMeasurementRearTubeTemp(Message As Message)
    '        If MeasurementMetricUpdatedEvent IsNot Nothing Then
    '            Dim xx As Integer = ((Message.Parameters(0) - 48) * 16) + (Message.Parameters(1) - 48)
    '            Dim Value As Integer = (xx - 136) * 2
    '            RaiseEvent MeasurementMetricUpdated(Me, New MeasurementMetricUpdatedEventArgs(MeasurementMetricTypes.RearTubeTemp, Value.ToString & "£C"))
    '        End If
    '    End Sub

    '    Private Sub HandleMessageMeasurementVSWR(Message As Message)
    '        If MeasurementMetricUpdatedEvent IsNot Nothing Then
    '            Dim xx As Integer = ((Message.Parameters(0) - 48) * 16) + (Message.Parameters(1) - 48)
    '            Dim yy As Integer = ((Message.Parameters(2) - 48) * 16) + (Message.Parameters(3) - 48)

    '            Dim OutputValue As String = ""
    '            If xx = 0 And yy = 0 Then
    '                OutputValue = "----"
    '            Else
    '                Dim Value As Double = (2.5 * xx + yy) / (2.5 * xx - yy)
    '                If Value > 9.8 Then
    '                    OutputValue = "Too High"
    '                Else
    '                    OutputValue = Value.ToString("0.00")
    '                End If
    '            End If
    '            RaiseEvent MeasurementMetricUpdated(Me, New MeasurementMetricUpdatedEventArgs(MeasurementMetricTypes.VSWR, OutputValue))
    '        End If
    '    End Sub

    '    Private Sub HandleMessageRCUEnteredMeasureMode()
    '        If RCUIsMeasuringEvent IsNot Nothing Then
    '            RaiseEvent RCUIsMeasuring(Me, New EventArgs)
    '        End If
    '    End Sub

    '    Private Sub HandleMessageStandbyModeOn()
    '        If StandbyModeOnEvent IsNot Nothing Then
    '            RaiseEvent StandbyModeOn(Me, New EventArgs)
    '        End If
    '    End Sub

    '    Private Sub HandleMessageOperateModeOn()
    '        If OperateModeOnEvent IsNot Nothing Then
    '            RaiseEvent OperateModeOn(Me, New EventArgs)
    '        End If
    '    End Sub

    '    Private Sub HandleMessagePowerOffComplete()
    '        If PowerOffCompleteEvent IsNot Nothing Then
    '            RaiseEvent PowerOffComplete(Me, New EventArgs)
    '        End If
    '    End Sub

    '    Private Sub HandleMessagePowerOffCoolingTubes()
    '        If PowerOffCoolingTubesEvent IsNot Nothing Then
    '            RaiseEvent PowerOffCoolingTubes(Me, New EventArgs)
    '        End If
    '    End Sub

    '    Private Sub HandleMessageHVOn()
    '        If HVOnExecutedEvent IsNot Nothing Then
    '            RaiseEvent HVOnExecuted(Me, New EventArgs)
    '        End If
    '    End Sub

    '    Private Sub HandleMessageBroadcastID(ByVal Message As Message)
    '        'Are we using autodetection and are we waiting to discover the ACOM2000A address
    '        If m_UseAutoDetection And Not m_ACOM2000Detected Then
    '            If Message.SourceAddress >= Constants.LOWEST_AMPLIFIER_ADDRESS And Message.SourceAddress <= Constants.HIGHEST_AMPLIFIER_ADDRESS Then
    '                'A Valid ACOM2000 Network address has been detected
    '                m_TimeoutTimer.Stop()
    '                m_ACOMNetworkACOM2000AAddress = Message.SourceAddress
    '                m_ACOM2000Detected = True
    '                If ACOM2000ADetectedEvent IsNot Nothing Then
    '                    RaiseEvent ACOM2000ADetected(Me, New DeviceDetectedEventArgs(Message.SourceAddress))
    '                End If
    '            End If
    '        End If
    '    End Sub

    '    Private Sub HandleMessageReflectedPowerLED(ByVal Message As Message)
    '        If ReflectedPowerLedUpdatedEvent IsNot Nothing Then
    '            Dim Args As New PowerLedChangedEventArgs(Message.Parameters(0) - Constants.REFLECTED_POWER_LED_MESSAGE_LOWESTVALUE)
    '            If ReflectedPowerLedUpdatedEvent IsNot Nothing Then
    '                RaiseEvent ReflectedPowerLedUpdated(Me, Args)
    '            End If
    '        End If
    '    End Sub

    '    Private Sub HandleMessageForwardPowerLED(ByVal Message As Message)
    '        If ForwardPowerLedUpdatedEvent IsNot Nothing Then
    '            Dim Args As New PowerLedChangedEventArgs(Message.Parameters(0) - Constants.FORWARD_POWER_LED_MESSAGE_LOWESTVALUE)
    '            If ForwardPowerLedUpdatedEvent IsNot Nothing Then
    '                RaiseEvent ForwardPowerLedUpdated(Me, Args)
    '            End If
    '        End If
    '    End Sub

    '    Private Sub HandleMessageCurrentSegmentInfo(ByVal Message As Message)

    '        If BandFrequencyChangedEvent IsNot Nothing Then
    '            Dim BandSegmentID As Integer = ((Message.MessageData(4) - 48) * 16) + (Message.MessageData(5) - 48)
    '            Dim Segment As BandSegment = BandSegment.GetSegment(BandSegmentID)
    '            Dim AntennaNumber As Integer = Message.MessageData(6) - 48
    '            Dim USRMode As Boolean = (Message.MessageData(7) - 48) = 1
    '            Dim Band As FrequencyBands = CType(Message.MessageData(8) - 48, FrequencyBands)

    '            RaiseEvent BandFrequencyChanged(Me, New BandFrequencyEventArgs(Segment, AntennaNumber, USRMode, Band))
    '        End If

    '    End Sub

    '    Private Sub HandleMessageAirTemperature(ByVal Message As Message)

    '        Dim NumberOfBars As Integer = Message.Parameters(0) - 48
    '        If NumberOfBars <= 13 Then
    '            If AirTemperatureChangedEvent IsNot Nothing Then
    '                RaiseEvent AirTemperatureChanged(Me, New AirTemperatureChangedEventArgs(NumberOfBars))
    '            End If
    '        Else
    '            Throw New AcomMessageException("Air Temperature message received with a number of bars value exceeding 13")
    '        End If

    '    End Sub

    '    Private Sub HandleMessageBANDMotorPosition(ByVal Message As Message)

    '        'Command Length should be 7
    '        If Not Message.MessageData.Length = 7 Then
    '            Throw New AcomMessageException("Invalid Message length for command:" & Message.MessageType)
    '        End If

    '        'Only fire the event if it is subscribed to
    '        If BandMotorPositionUpdatedEvent IsNot Nothing Then
    '            'Get the Sensor setting
    '            Dim SensorFlag As String = Chr(Message.Parameters(3))
    '            Dim Sensor As MotorSensorStates
    '            If SensorFlag = "L" Then
    '                Sensor = MotorSensorStates.Light
    '            ElseIf SensorFlag = "D" Then
    '                Sensor = MotorSensorStates.Dark
    '            Else
    '                Throw New AcomMessageException("Unknown sensor setting received from response message A, sensor flag should be either L or D but the value " & SensorFlag & " was received")
    '            End If

    '            'Get the position 
    '            Dim PositionString As String = Chr(Message.Parameters(0)) & Chr(Message.Parameters(1)) & Chr(Message.Parameters(2))
    '            Dim Position As Integer = CInt(PositionString)

    '            RaiseEvent BandMotorPositionUpdated(Me, New MotorPositionChangedEventArgs(Position, Sensor))
    '        End If

    '    End Sub

    '    Private Sub HandleMessageLOADMotorPosition(ByVal Message As Message)

    '        'Command Length should be 7
    '        If Not Message.MessageData.Length = 7 Then
    '            Throw New AcomMessageException("Invalid Message length for command:" & Message.MessageType)
    '        End If

    '        'Only fire the event if it is subscribed to
    '        If LoadMotorPositionUpdatedEvent IsNot Nothing Then
    '            'Get the Sensor setting
    '            Dim SensorFlag As String = Chr(Message.MessageData(6))
    '            Dim Sensor As MotorSensorStates
    '            If SensorFlag = "L" Then
    '                Sensor = MotorSensorStates.Light
    '            ElseIf SensorFlag = "D" Then
    '                Sensor = MotorSensorStates.Dark
    '            Else
    '                Throw New AcomMessageException("Unknown sensor setting received from response message B, sensor flag should be either L or D but the value " & SensorFlag & " was received")
    '            End If

    '            'Get the position 
    '            Dim PositionString As String = Chr(Message.MessageData(3)) & Chr(Message.MessageData(4)) & Chr(Message.MessageData(5))
    '            Dim Position As Integer = CInt(PositionString)

    '            RaiseEvent LoadMotorPositionUpdated(Me, New MotorPositionChangedEventArgs(Position, Sensor))
    '        End If

    '    End Sub

    '    Private Sub HandleMessageTUNEMotorPosition(ByVal Message As Message)

    '        'Command Length should be 7
    '        If Not Message.MessageData.Length = 7 Then
    '            Throw New AcomMessageException("Invalid Message length for command:" & Message.MessageType)
    '        End If

    '        'Only fire the event if it is subscribed to
    '        If TuneMotorPositionUpdatedEvent IsNot Nothing Then
    '            'Get the Sensor setting
    '            Dim SensorFlag As String = Chr(Message.MessageData(6))
    '            Dim Sensor As MotorSensorStates
    '            If SensorFlag = "L" Then
    '                Sensor = MotorSensorStates.Light
    '            ElseIf SensorFlag = "D" Then
    '                Sensor = MotorSensorStates.Dark
    '            Else
    '                Throw New AcomMessageException("Unknown sensor setting received from response message C, sensor flag should be either L or D but the value " & SensorFlag & " was received")
    '            End If

    '            'Get the position 
    '            Dim PositionString As String = Chr(Message.MessageData(3)) & Chr(Message.MessageData(4)) & Chr(Message.MessageData(5))
    '            Dim Position As Integer = CInt(PositionString)

    '            RaiseEvent TuneMotorPositionUpdated(Me, New MotorPositionChangedEventArgs(Position, Sensor))
    '        End If

    '    End Sub

    '    Private Sub HandleMessageLEDStatus(ByVal Message As Message)

    '        'Uses the Lower 4 Bytes only to signify status 

    '        If LEDStatusUpdateEvent IsNot Nothing Then
    '            'Protection LED
    '            Dim ProtectionLED As LedStatus = LedStatus.Off
    '            If CBool(Message.MessageData(3) And &H4) Then
    '                ProtectionLED = LedStatus.Blinking
    '            ElseIf CBool(Message.MessageData(3) And &HC) Then
    '                ProtectionLED = LedStatus.On
    '            End If

    '            'Header LED
    '            Dim HeaterLED As LedStatus = LedStatus.Off
    '            If CBool(Message.MessageData(3) And &H1) Then
    '                HeaterLED = LedStatus.Blinking
    '            ElseIf CBool(Message.MessageData(3) And &H3) Then
    '                HeaterLED = LedStatus.On
    '            End If

    '            'Standby LED
    '            Dim StandByLED As LedStatus = LedStatus.Off
    '            If Not CBool(Message.MessageData(4) And &H1) Then
    '                StandByLED = LedStatus.On
    '            End If

    '            'Operate LED
    '            Dim OperateLED As LedStatus = LedStatus.Off
    '            If CBool(Message.MessageData(4) And &H1) Then
    '                OperateLED = LedStatus.On
    '            End If

    '            'Drive LED
    '            Dim DriveLED As LedStatus = LedStatus.Off
    '            If CBool(Message.MessageData(4) And &H2) Then
    '                DriveLED = LedStatus.On
    '            End If

    '            'Plate LED
    '            Dim PlateLED As LedStatus = LedStatus.Off
    '            If CBool(Message.MessageData(4) And &H4) Then
    '                PlateLED = LedStatus.On
    '            End If

    '            RaiseEvent LEDStatusUpdate(Me, New LEDStatusEventArgs(ProtectionLED, HeaterLED, StandByLED, OperateLED, DriveLED, PlateLED))
    '        End If

    '    End Sub

    '    Private Sub HandleMessagePowerOnExecuted()
    '        If PowerOnExecutedEvent IsNot Nothing Then
    '            RaiseEvent PowerOnExecuted(Me, New EventArgs)
    '        End If
    '    End Sub

    '    Private Sub HandleMessagePowerOffExecuted()
    '        If PowerOffExecutedEvent IsNot Nothing Then
    '            RaiseEvent PowerOffExecuted(Me, New EventArgs)
    '        End If
    '    End Sub

    '    Private Sub HandleMessageEEPROMContents(ByVal Message As Message)

    '        'TODO: This needs completing
    '        'Please note most of this routine is commented out until it's completed to avoid the code analysis generating unneccesary warnings

    '        'Get the EEPROM Segment Number
    '        Dim SegmentNumber As Integer = Message.MessageData(4) - 48
    '        If SegmentNumber < 0 Or SegmentNumber > 7 Then
    '            Throw New AcomMessageException("Unexpected Segment Number from response message G3, reported segment " & SegmentNumber.ToString & " when the value should be between 0 and 7")
    '        End If

    '        Select Case SegmentNumber
    '            Case 0
    '                '        If AmplifierInfoReceivedEvent IsNot Nothing Then
    '                '            Dim AmplifierSerial As String = Chr(Message.MessageData(5)) & Chr(Message.MessageData(6)) & Chr(Message.MessageData(7)) & Chr(Message.MessageData(8)) & Chr(Message.MessageData(9)) & Chr(Message.MessageData(10))
    '                '            Dim RCUSerial As String = Chr(Message.MessageData(11)) & Chr(Message.MessageData(12)) & Chr(Message.MessageData(13)) & Chr(Message.MessageData(14)) & Chr(Message.MessageData(15)) & Chr(Message.MessageData(16))
    '                '            Dim FrontTubeSerial As String = Chr(Message.MessageData(17)) & Chr(Message.MessageData(18)) & Chr(Message.MessageData(19)) & Chr(Message.MessageData(20)) & Chr(Message.MessageData(21)) & Chr(Message.MessageData(22))
    '                '            Dim RearTubeSerial As String = Chr(Message.MessageData(23)) & Chr(Message.MessageData(24)) & Chr(Message.MessageData(25)) & Chr(Message.MessageData(26)) & Chr(Message.MessageData(27)) & Chr(Message.MessageData(28))
    '                '            Dim LicenceStatus As String = Chr(Message.MessageData(29))
    '                '            Dim PowerOnTimeSeconds As Integer = CInt("&H" & Hex(Message.MessageData(30) - 48) & Hex(Message.MessageData(31) - 48) & Hex(Message.MessageData(32) - 48) & Hex(Message.MessageData(33) - 48) & Hex(Message.MessageData(34) - 48) & Hex(Message.MessageData(35) - 48) & Hex(Message.MessageData(36) - 48))

    '                '            RaiseEvent AmplifierInfoReceived(Me, New AmplifierInfoEventArgs(AmplifierSerial, RCUSerial, FrontTubeSerial, RearTubeSerial, LicenceStatus, PowerOnTimeSeconds))
    '                '        End If
    '            Case 1

    '                '        Dim RCUFirmwareRevision As String = Chr(Message.MessageData(5))
    '                '        Dim CPUFirmwareRevision As String = Chr(Message.MessageData(6))
    '                '        Dim EEPROM As String = Chr(Message.MessageData(7)) & Chr(Message.MessageData(8))
    '                '        Dim Callsign As String = _
    '                '            Chr(((Message.MessageData(9) - 48) * 16) + (Message.MessageData(10) - 48)) & _
    '                '            Chr(((Message.MessageData(11) - 48) * 16) + (Message.MessageData(12) - 48)) & _
    '                '            Chr(((Message.MessageData(13) - 48) * 16) + (Message.MessageData(14) - 48)) & _
    '                '            Chr(((Message.MessageData(15) - 48) * 16) + (Message.MessageData(16) - 48)) & _
    '                '            Chr(((Message.MessageData(17) - 48) * 16) + (Message.MessageData(18) - 48)) & _
    '                '            Chr(((Message.MessageData(19) - 48) * 16) + (Message.MessageData(20) - 48)) & _
    '                '            Chr(((Message.MessageData(21) - 48) * 16) + (Message.MessageData(22) - 48)) & _
    '                '            Chr(((Message.MessageData(23) - 48) * 16) + (Message.MessageData(24) - 48)) & _
    '                '            Chr(((Message.MessageData(25) - 48) * 16) + (Message.MessageData(26) - 48))

    '                '        'TODO: Need to strip unused Characters from the callsign 

    '                '        Dim Key As String = Chr(Message.MessageData(27))


    '                '        Dim ErrorCode As String = Chr(Message.MessageData(28)) & Chr(Message.MessageData(29))
    '                '        Dim ErrorParam As String = Chr(Message.MessageData(30)) & Chr(Message.MessageData(31))
    '                '        Dim Antenna As String = Chr(Message.MessageData(32))


    '                '        'Dim Time As Integer = CInt("&H" & Hex(CurrentCommand(31) - 48) & Hex(CurrentCommand(32) - 48) & Hex(CurrentCommand(33) - 48) & Hex(CurrentCommand(34) - 48) & Hex(CurrentCommand(35) - 48) & Hex(CurrentCommand(36) - 48) & Hex(CurrentCommand(37) - 48))
    '                '        'Dim Segment As String


    '            Case 2, 3, 4, 5, 6

    '                '        If UnknownMessageEvent IsNot Nothing Then
    '                '            RaiseEvent UnknownMessage(Me, New UnknownMessageEventArgs("UNKNOWN EEPROM CONTENTS"))
    '                '        End If

    '            Case 7

    '                '        'CurrentCommand(5) - DOES NOT APPEAR TO BE USED
    '                '        'CurrentCommand(6) - DOES NOT APPEAR TO BE USED
    '                '        Dim MainsVoltageSelection As String = Chr(Message.MessageData(7))
    '                '        'CurrentCommand(8) - DOES NOT APPEAR TO BE USED
    '                '        Dim AccessCode As String = _
    '                '            Chr(((Message.MessageData(9) - 48) * 16) + (Message.MessageData(10) - 48)) & _
    '                '            Chr(((Message.MessageData(11) - 48) * 16) + (Message.MessageData(12) - 48)) & _
    '                '            Chr(((Message.MessageData(13) - 48) * 16) + (Message.MessageData(14) - 48)) & _
    '                '            Chr(((Message.MessageData(15) - 48) * 16) + (Message.MessageData(16) - 48)) & _
    '                '            Chr(((Message.MessageData(17) - 48) * 16) + (Message.MessageData(18) - 48)) & _
    '                '            Chr(((Message.MessageData(19) - 48) * 16) + (Message.MessageData(20) - 48))

    '                '        Dim LocalNetworkAddress As Integer = ((Message.MessageData(21) - 48) * 16) + (Message.MessageData(22) - 48)

    '                '        Dim AutoAntennaChange As String = (((Message.MessageData(23) - 48) * 16) + (Message.MessageData(24) - 48)).ToString

    '                '        'CurrentCommand(25) -> CurrentCommand(36) - DOES NOT APPEAR TO BE USED



    '        End Select

    '    End Sub

    '    Private Sub HandleMessageAmplifierError(ByVal Message As Message)

    '        If AmplifierErrorEvent IsNot Nothing Then
    '            Dim ErrorCode As Integer = ((Message.Parameters(0) - 48) * 16) + (Message.Parameters(1) - 48)
    '            Dim RAWParameter As Integer = 0
    '            If Not Message.Parameters(2) = 0 And Not Message.Parameters(3) = 0 Then
    '                RAWParameter = ((Message.Parameters(2) - 48) * 16) + (Message.Parameters(3) - 48)
    '            End If

    '            Select Case ErrorCode
    '                Case 1 '= COVER INTERLOCK OPEN
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("COVER INTERLOCK OPEN", AmplifierErrorParameterTypes.None, ""))
    '                Case 2 '= +5V TOO HIGH:                                  V
    '                    'TODO: Need to determine the calculation for the parameter
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("+5V TOO HIGH", AmplifierErrorParameterTypes.None, ""))
    '                Case 3 '= +12V TOO HIGH:                                 V
    '                    'TODO: Need to determine the calculation for the parameter
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("+12V TOO HIGH", AmplifierErrorParameterTypes.None, ""))
    '                Case 4 '= -12V TOO HIGH:                                 V
    '                    'TODO: Need to determine the calculation for the parameter
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("-12V TOO HIGH", AmplifierErrorParameterTypes.None, ""))
    '                Case 5 '= LINE VOLTAGE TOO HIGH:                         V
    '                    'TODO: Need to determine the calculation for the parameter
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("LINE VOLTAGE|TOO HIGH", AmplifierErrorParameterTypes.None, ""))
    '                Case 7 '= SCREEN VOLTAGE ON BEFORE TIMEOUT OVER:         V
    '                    'TODO: Need to determine the calculation for the parameter
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("SCREEN VOLTAGE ON|BEFORE TIMEOUT OVER", AmplifierErrorParameterTypes.None, ""))
    '                Case 8 '= IG2 PRESENT: IT SHOULD BE ZERO:                mA
    '                    'TODO: Need to determine the calculation for the parameter
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("IG2 PRESENT|IT SHOULD BE ZERO", AmplifierErrorParameterTypes.None, ""))
    '                Case 9 '= IG1 PRESENT: IT SHOULD BE ZERO:                mA
    '                    'TODO: Need to determine the calculation for the parameter
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("IG1 PRESENT|IT SHOULD BE ZERO", AmplifierErrorParameterTypes.None, ""))
    '                Case 10 '= Ip PRESENT: IT SHOULD BE ZERO:                 mA
    '                    'TODO: Need to determine the calculation for the parameter
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("Ip PRESENT|IT SHOULD BE ZERO", AmplifierErrorParameterTypes.None, ""))
    '                Case 13 '= +48V TOO HIGH:                                 V
    '                    'TODO: Need to determine the calculation for the parameter
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("+48V TOO HIGH", AmplifierErrorParameterTypes.None, ""))
    '                Case 14 '= ANTENNA RELAY CLOSED: IT SHOULD BE OPEN
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("ANTENNA RELAY CLOSED|IT SHOULD BE OPEN", AmplifierErrorParameterTypes.None, ""))
    '                Case 15 '= INPUT RELAY CLOSED: IT SHOULD BE OPEN
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("INPUT RELAY CLOSED|IT SHOULD BE OPEN", AmplifierErrorParameterTypes.None, ""))
    '                Case 16 '= QSK BREAK in FAULT: PROCESSOR CONFLICT
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("QSK BREAK in FAULT|PROCESSOR CONFLICT", AmplifierErrorParameterTypes.None, ""))
    '                Case 17 '= RF PLATE PRESENT: IT SHOULD BE ZERO:           V
    '                    'TODO: Need to determine the calculation for the parameter
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("RF PLATE PRESENT|IT SHOULD BE ZERO", AmplifierErrorParameterTypes.None, ""))
    '                Case 18 '= RF GRID POWER PRESENT:IT SHOULD BE ZERO:       W
    '                    'TODO: Need to determine the calculation for the parameter
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("RF GRID POWER PRESENT|IT SHOULD BE ZERO", AmplifierErrorParameterTypes.None, ""))
    '                Case 19 '= HEATERS ON BEFORE TIMEOUT STARTED
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("HEATERS ON BEFORE|TIMEOUT STARTED", AmplifierErrorParameterTypes.None, ""))
    '                Case 21 '= REAR TUBE EXHAUST TEMP TOO HIGH:               oC
    '                    Dim Parameter As Integer = 0
    '                    Parameter = (RAWParameter - 136) * 2
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("REAR TUBE EXHAUST|TEMP TOO HIGH $var$", AmplifierErrorParameterTypes.DegC, Parameter.ToString & "£C"))
    '                Case 22 '= FRONT TUBE EXHAUST TEMP TOO HIGH:              oC
    '                    Dim Parameter As Integer = 0
    '                    Parameter = (RAWParameter - 136) * 2
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("FRONT TUBE EXHAUST|TEMP TOO HIGH $var$", AmplifierErrorParameterTypes.DegC, Parameter.ToString & "£C"))
    '                Case 23 '= HV TOO LOW AT POWER ON:                        V
    '                    'TODO: Need to determine the calculation for the parameter
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("HV TOO LOW|AT POWER ON", AmplifierErrorParameterTypes.None, ""))
    '                Case 24 '= BIAS TOO LOW AT POWER ON:                      V
    '                    'TODO: Need to determine the calculation for the parameter
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("BIAS TOO LOW|AT POWER ON", AmplifierErrorParameterTypes.None, ""))
    '                Case 26 '= G2: CURRENT TOO HIGH:                          mA
    '                    'TODO: Need to determine the calculation for the parameter
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("G2|CURRENT TOO HIGH", AmplifierErrorParameterTypes.None, ""))
    '                Case 27 '= G1: EXCESSIVE GRID CURRENT:                    mA
    '                    'TODO: Need to determine the calculation for the parameter
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("G1|EXCESSIVE GRID CURRENT", AmplifierErrorParameterTypes.None, ""))
    '                Case 30 '= Ip PRESENT: SHOULD BE ZERO:                    mA
    '                    'TODO: Need to determine the calculation for the parameter
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("Ip PRESENT|SHOULD BE ZERO", AmplifierErrorParameterTypes.None, ""))
    '                Case 31 '= HEATER VOLTAGE TOO HIGH:                       V
    '                    'TODO: Need to determine the calculation for the parameter
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("HEATER VOLTAGE|TOO HIGH", AmplifierErrorParameterTypes.None, ""))
    '                Case 33 '= HEATER CURRENT TOO HIGH:                       A
    '                    'TODO: Need to determine the calculation for the parameter
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("HEATER CURRENT|TOO HIGH", AmplifierErrorParameterTypes.None, ""))
    '                Case 35 '= BIAS VOLTAGE TOO LOW:                          V
    '                    'TODO: Need to determine the calculation for the parameter
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("BIAS VOLTAGE TOO LOW", AmplifierErrorParameterTypes.None, ""))
    '                Case 36 '= G2 CONTROL CIRCUIT MALFUNCTION
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("G2 CONTROL|CIRCUIT MALFUNCTION", AmplifierErrorParameterTypes.None, ""))
    '                Case 37 '= AUTOMATIC PROTECTION:DRIVE POWER REMOVED
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("AUTOMATIC PROTECTION|DRIVE POWER REMOVED", AmplifierErrorParameterTypes.None, ""))
    '                Case 38 '= REMOVE AND REDUCE DRIVE POWER
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("REMOVE AND REDUCE|DRIVE POWER", AmplifierErrorParameterTypes.None, ""))
    '                Case 39 '= HIGH VOLTAGE TOO LOW:                          kV
    '                    'TODO: Need to determine the calculation for the parameter
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("HIGH VOLTAGE TOO LOW", AmplifierErrorParameterTypes.None, ""))
    '                Case 40 '= G2 VOLTAGE TOO LOW:                            V
    '                    'TODO: Need to determine the calculation for the parameter
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("G2 VOLTAGE TOO LOW", AmplifierErrorParameterTypes.None, ""))
    '                Case 41 '= HIGH VOLTAGE TOO HIGH:                         kV
    '                    'TODO: Need to determine the calculation for the parameter
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("HIGH VOLTAGE|TOO HIGH", AmplifierErrorParameterTypes.None, ""))
    '                Case 42 '= HEATER VOLTAGE TOO LOW:                        V
    '                    'TODO: Need to determine the calculation for the parameter
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("HEATER VOLTAGE|TOO LOW", AmplifierErrorParameterTypes.None, ""))
    '                Case 43 '= HEATER CURRENT TOO LOW:                        A
    '                    'TODO: Need to determine the calculation for the parameter
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("HEATER CURRENT|TOO LOW", AmplifierErrorParameterTypes.None, ""))
    '                Case 45 '= LOW AIRFLOW
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("LOW AIRFLOW", AmplifierErrorParameterTypes.None, ""))
    '                Case 46 '= ANTENNA RELAY OPEN:IT SHOULD BE CLOSED
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("ANTENNA RELAY OPEN|IT SHOULD BE CLOSED", AmplifierErrorParameterTypes.None, ""))
    '                Case 47 '= DRIVE POWER TOO HIGH:                          W
    '                    'TODO: Need to determine the calculation for the parameter
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("DRIVE POWER TOO HIGH", AmplifierErrorParameterTypes.None, ""))
    '                Case 48 '= REFLECTED POWER TOO HIGH:                      W
    '                    Dim Parameter As Integer = 0
    '                    Parameter = CInt(Math.Floor((RAWParameter * RAWParameter) / 100))
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("REFLECTED POWER TOO|HIGH = $var$", AmplifierErrorParameterTypes.Watts, Parameter.ToString & "W"))
    '                Case 49 '= EXCESSIVE PLATE CURRENT:                       mA
    '                    'TODO: Need to determine the calculation for the parameter
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("EXCESSIVE|PLATE CURRENT", AmplifierErrorParameterTypes.None, ""))
    '                Case 50 '= EXCESSIVE G2 CURRENT:                          mA
    '                    'TODO: Need to determine the calculation for the parameter
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("EXCESSIVE|G2 CURRENT", AmplifierErrorParameterTypes.None, ""))
    '                Case 54 '= ARC FAULT
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("ARC FAULT", AmplifierErrorParameterTypes.None, ""))
    '                Case 55 '= DRIVE FREQUENCY OUT OF RANGE
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("DRIVE FREQUENCY|OUT OF RANGE", AmplifierErrorParameterTypes.None, ""))
    '                Case 58 '= BIAS CONTROL CIRQUIT MALFUNCTION
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("BIAS CONTROL|CIRQUIT MALFUNCTION", AmplifierErrorParameterTypes.None, ""))
    '                Case 59 '= BAND SWITCH SYSTEM MALFUNCTION
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("BAND SWITCH|SYSTEM MALFUNCTION", AmplifierErrorParameterTypes.None, ""))
    '                Case 60 '= DETECTED RF POWER AT WRONG TIME
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("DETECTED RF POWER|AT WRONG TIME", AmplifierErrorParameterTypes.None, ""))
    '                Case 61 '= ADC - OVERFLOW
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("ADC - OVERFLOW", AmplifierErrorParameterTypes.None, ""))
    '                Case 62 '= MICROPROCESSOR CONFLICT
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("MICROPROCESSOR|CONFLICT", AmplifierErrorParameterTypes.None, ""))
    '                Case 63 '= FREQUENCY VIOLATION
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("FREQUENCY VIOLATION", AmplifierErrorParameterTypes.None, ""))
    '                Case 65 '= LOW GAIN
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("LOW GAIN", AmplifierErrorParameterTypes.None, ""))
    '                Case 66 '= +5V TOO LOW:                                   V
    '                    'TODO: Need to determine the calculation for the parameter
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("+5V TOO LOW", AmplifierErrorParameterTypes.None, ""))
    '                Case 67 '= +12V TOO LOW:                                  V
    '                    'TODO: Need to determine the calculation for the parameter
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("+12V TOO LOW", AmplifierErrorParameterTypes.None, ""))
    '                Case 68 '= -12V TOO LOW:                                  V
    '                    'TODO: Need to determine the calculation for the parameter
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("-12V TOO LOW", AmplifierErrorParameterTypes.None, ""))
    '                Case 69 '= LINE AC VOLTAGE TOO LOW:                       V
    '                    'TODO: Need to determine the calculation for the parameter
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("LINE AC VOLTAGE|TOO LOW", AmplifierErrorParameterTypes.None, ""))
    '                Case 70 '= +48V TOO LOW:                                  V
    '                    'TODO: Need to determine the calculation for the parameter
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("+48V TOO LOW", AmplifierErrorParameterTypes.None, ""))
    '                Case 71 '= G2 VOLTAGE TOO HIGH:                           V
    '                    'TODO: Need to determine the calculation for the parameter
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("G2 VOLTAGE TOO HIGH", AmplifierErrorParameterTypes.None, ""))
    '                Case 72 '= AUTO PROTECTION: ARC FAULT 5 TIMES
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("AUTO PROTECTION|ARC FAULT 5 TIMES", AmplifierErrorParameterTypes.None, ""))
    '                Case 73 '= TUNE CAPACITOR SYSTEM MALFUNCTION
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("TUNE CAPACITOR|SYSTEM MALFUNCTION", AmplifierErrorParameterTypes.None, ""))
    '                Case 74 '= LOAD CAPACITOR SYSTEM MALFUNCTION
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("LOAD CAPACITOR|SYSTEM MALFUNCTION", AmplifierErrorParameterTypes.None, ""))
    '                Case 75 '= MOTOR VOLTAGE TOO LOW:                         V
    '                    'TODO: Need to determine the calculation for the parameter
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("MOTOR VOLTAGE|TOO LOW", AmplifierErrorParameterTypes.None, ""))
    '                Case 76 '= MOTOR VOLTAGE TOO HIGH:                        V
    '                    'TODO: Need to determine the calculation for the parameter
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("MOTOR VOLTAGE|TOO HIGH", AmplifierErrorParameterTypes.None, ""))
    '                Case 77 '= +24V TOO LOW:                                  V
    '                    'TODO: Need to determine the calculation for the parameter
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("+24V TOO LOW", AmplifierErrorParameterTypes.None, ""))
    '                Case 78 '= +24V TOO HIGH:                                 V
    '                    'TODO: Need to determine the calculation for the parameter
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("+24V TOO HIGH", AmplifierErrorParameterTypes.None, ""))
    '                Case 80 '= ANTENNA CONTROL UNIT MALFUNCTION
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("ANTENNA CONTROL|UNIT MALFUNCTION", AmplifierErrorParameterTypes.None, ""))
    '                Case Else
    '                    RaiseEvent AmplifierError(Me, New AmplifierErrorEventArgs("UNKNOWN ERROR|CODE " & ErrorCode.ToString(), AmplifierErrorParameterTypes.None, ""))
    '            End Select
    '        End If

    '    End Sub

    '#End Region

#End Region


    Private Sub m_BackgroundSenderTimer_Elapsed(sender As Object, e As Timers.ElapsedEventArgs) Handles m_BackgroundSenderTimer.Elapsed

        'Send Self Identifier Broadcast
        SendMessage(GenerateBroadcastMessageFrame("L"), True)

    End Sub

End Class
